package cn.net.shuxing.yifu.common.service;

import cn.net.shuxing.yifu.common.constant.OssConstant;
import cn.net.shuxing.yifu.common.util.JedisUtil;
import cn.net.shuxing.yifu.common.util.MarkImageUtil;
import cn.net.shuxing.yifu.common.util.RedisKeyCreator;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Base64Utils;
import cn.net.shuxing.yifu.common.util.StringUtils;
import redis.clients.jedis.Jedis;
import sun.misc.BASE64Encoder;

import javax.imageio.ImageIO;
import javax.servlet.ServletContext;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

@Service
public class MarkImageService  {

    @Autowired
    private ServletContext servletContext;

//    public static int point;



    @Autowired
    private JedisUtil jedisUtil;

    @Autowired
    private OssCommonService ossCommonService;

    public Map<String,String> generateMarkImage(Integer userId)
    {
        Random random = new Random();
        int index = Math.abs(random.nextInt() % 100) + 1001;
        //Jedis //jedis = jedisUtil.getJedis();
        String s = jedisUtil.get(RedisKeyCreator.currentMarkImageIndex());

//        originImage1071originImage.png
//        originImage1071originImage.png

        String os = "originImage" + index + "originImage.png";
        String originImageUrl =ossCommonService.getUrl(OssConstant.ossBucketName,os);
        String ss = "sliderImage" + index + "sliderImage.png";
        String sliderImageUrl =ossCommonService.getUrl(OssConstant.ossBucketName,ss);
        Map<String,String> map = new HashMap<>();
        map.put("originImageUrl",originImageUrl);
        map.put("sliderImageUrl",sliderImageUrl);
        map.put("index",String.valueOf(index));
        return map;
    }

    public boolean verifyPosition(int point, int index)
    {
        //Jedis //jedis = jedisUtil.getJedis();
        String s = jedisUtil.get(RedisKeyCreator.sliderImagePosition(index));
        if (!StringUtils.hasText(s))
            return false;
        int x = Integer.valueOf(s);
        return point >= x - 20 && point <= x + 20;
    }
}
